package com.caregiver.watch.business.data.parser;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * @Description:
 * @Author 疆戟
 * @Date 2025/9/6 14:48
 * @Version 1.0
 */
@Component
@Slf4j
public class DataParserContext {

    private final Map<Integer, IDataParser> optToDataParserMapping;

    public DataParserContext(List<IDataParser> dataParsers) {
        this.optToDataParserMapping = Optional.ofNullable(dataParsers)
                .orElse(new ArrayList<>())
                .stream()
                .filter(dataParser -> Objects.nonNull(dataParser.getOpt()))
                .collect(Collectors.toMap(IDataParser::getOpt, dataParser -> dataParser, (v1, v2) -> v1));
    }


    public IDataParser getDataParser(Integer opt) {
        return optToDataParserMapping.get(opt);
    }
}
